package com.mallcai.bigdata.ladon.dpl.mapper.common.provider;

import com.mallcai.bigdata.ladon.dpl.entity.common.CommonInfo;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import com.mallcai.bigdata.ladon.dpl.mapper.system.provider.UserSqlProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class CommonInfoSqlProvider extends BaseSqlProvider<CommonInfo> {
    public static final String TB_COMMON_INFO = "c_common_info";


    @Override
    public String getTableName() {
        return TB_COMMON_INFO;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("classify", "#{classify}");
        map.put("`code`", "#{code}");
        map.put("name", "#{name}");
        map.put("`parent_classify`", "#{parentClassify}");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("classify", "classify");
        map.put("code", "`code`");
        map.put("name", "name");
        map.put("parentClassify", "`parent_classify`");
        return map;
    }

    /**
     * 根据参数查询
     */
    public String getByParams(Map<String, Object> map) {
        Map<String, Object> params = getParams(map);

        StringBuilder sb = new StringBuilder();
        sb.append("tb_c.archive = 0");
        if (params.containsKey("name") && StringUtils.isNotBlank(params.get("name").toString())) {
            sb.append(" and tb_c.name LIKE concat('%',#{params.name},'%')");
        }
        if (params.containsKey("code") && StringUtils.isNotBlank(params.get("code").toString())) {
            sb.append(" and tb_c.code LIKE concat('%',#{params.code},'%')");
        }
        if (params.containsKey("classify") && StringUtils.isNotBlank(params.get("classify").toString())) {
            sb.append(" and tb_c.classify = #{params.classify}");
        }
        if (params.containsKey("parentClassify") && StringUtils.isNotBlank(params.get("parentClassify").toString())) {
            sb.append(" and tb_c.parent_classify = #{params.parentClassify}");
        }

        if (params.containsKey("parentClassifyList") && params.get("parentClassifyList") instanceof Collection) {
            Collection parentClassifyList = (Collection)  params.get("parentClassifyList");
            if (!CollectionUtils.isEmpty(parentClassifyList)){
                Object collect = parentClassifyList.stream().map(item -> String.format("'%s'", item.toString()))
                        .collect(Collectors.joining(","));
                sb.append(" and tb_c.parent_classify in( " + collect  +" ) ");
            }
        }

        if (params.containsKey("createdBy")) {
            sb.append(" and tb_c.created_by = #{params.createdBy}");
        }
        return new SQL() {
            {
                SELECT("tb_u.nickname  AS creator, tb_c.*");
                FROM(getTableName() + " AS tb_c");
                LEFT_OUTER_JOIN(UserSqlProvider.TB_USER + " tb_u ON tb_u.id = tb_c.created_by");
                WHERE(sb.toString());
            }
        }.toString();
    }

    public String hasExist(@Param("classify") String classify, @Param("code") String code, @Param("name") String name, @Param("filterId") Long filterId) {
        StringBuilder sb = new StringBuilder();
        sb.append(" archive  = 0 AND classify = #{classify} AND `code` = #{code} AND name = #{name} ");
        if (filterId != null) {
            sb.append(" AND id != #{filterId}");
        }
        return new SQL() {
            {
                SELECT("COUNT(*)");
                FROM(getTableName());
                WHERE(sb.toString());
            }
        }.toString();
    }
}
